package com.polaris.live.adapter

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.blankj.utilcode.util.ColorUtils
import com.blankj.utilcode.util.StringUtils
import com.bumptech.glide.Glide
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.polaris.live.common.adapter.BaseAdapter
import com.polaris.live.databinding.AdapterTaskAnchor1Binding
import com.polaris.live.databinding.AdapterTaskAnchorProgressBinding
import com.polaris.live.dialog.common.TipsDialog
import com.polaris.live.resp.back_resp.UserTask
import com.polaris.live.translation.R
import com.polaris.live.utils.image.ImageUtils
import com.polaris.live.widget.refresh.BaseBindingViewHolder

/**
 * TaskCenterAnchorAdapter 主播任务adapter
 * @author Created by 天晴 on 2024/1/18 19:27
 * @since 1.0.0
 **/
class TaskCenterAnchorAdapter : BaseAdapter<UserTask, BaseViewHolder>(0) {

    override fun getDefItemViewType(position: Int): Int {
        val item = data[position]
        return when (item.progressType) {
            0L -> TYPE_TASK_1
            1L -> TYPE_TASK_PROGRESS
            else -> throw IllegalArgumentException("Invalid view type: ${item.progressType}")
        }

    }

    override fun onCreateDefViewHolder(
        parent: ViewGroup,
        viewType: Int,
    ): BaseBindingViewHolder<*, *> {
        val layoutInflater = LayoutInflater.from(parent.context)
        return when (viewType) {
            TYPE_TASK_1 -> TaskCenterAnchorViewHolder(
                AdapterTaskAnchor1Binding.inflate(layoutInflater, parent, false)
            )

            TYPE_TASK_PROGRESS -> TaskCenterAnchorProgressViewHolder(
                AdapterTaskAnchorProgressBinding.inflate(layoutInflater, parent, false)
            )

            else -> throw IllegalArgumentException("Invalid view type: $viewType")
        }
    }

    override fun convert(holder: BaseViewHolder, item: UserTask) {
        when (holder) {
            is TaskCenterAnchorViewHolder -> {
                holder.bind(item)
                holder.setItemListener({
                    clickBack?.invoke(it)
                }) {
                    goBack?.invoke(it)
                }
            }

            is TaskCenterAnchorProgressViewHolder -> {
                holder.bind(item)
                holder.setItemListener({
                    clickBack?.invoke(it)
                }) {
                    goBack?.invoke(it)
                }
            }
        }
    }


    class TaskCenterAnchorViewHolder(
        binding: AdapterTaskAnchor1Binding,
    ) : BaseBindingViewHolder<AdapterTaskAnchor1Binding, UserTask>(binding) {
        override fun bind(item: UserTask) {
            if (absoluteAdapterPosition == 0) {
                mBinding.bg.setBackgroundResource(com.polaris.live.R.drawable.rb_12_fff)
            } else {
                mBinding.bg.setBackgroundResource(com.polaris.live.R.drawable.r12_back_ffffff)
            }
            mBinding.apply {
                Glide.with(iconImg.context)
                    .load(ImageUtils.imgPath(item.photoUrl))
                    .into(iconImg)
                titleTv.text = item.name
                Glide.with(img.context)
                    .load(ImageUtils.imgPath(item.rewardIconUrl))
                    .into(img)
                tvNum.text = "x${item.rewardAmount}"

                mBinding.question.visibility = if (item.explain.isEmpty()) View.GONE else View.VISIBLE
                mBinding.question.setOnClickListener {
                    TipsDialog.show(mBinding.question.context, item.name, item.explain)
                }
                val rewards = item.rewards.getOrNull(0)
                rewards?.let {
                    state2Tv.text = it.unit

                    when (it.status) {
                        0L -> { //进行中
                            stateTv.text = StringUtils.getString(R.string.go_to_finish)
                            stateTv.setTextColor(ColorUtils.getColor(com.polaris.live.R.color.color_893AFF))
                            state2Tv.setTextColor(ColorUtils.getColor(com.polaris.live.R.color.black))
                            stateTv.setBackgroundResource(com.polaris.live.R.drawable.r20_shape_893aff_stork)
                        }

                        1L -> { //可领取
                            stateTv.text = StringUtils.getString(R.string.lucky_bag_bar_claimable)
                            stateTv.setTextColor(ColorUtils.getColor(com.polaris.live.R.color.white))
                            state2Tv.setTextColor(ColorUtils.getColor(com.polaris.live.R.color.black))
                            stateTv.setBackgroundResource(
                                com.polaris.live.R.drawable.shape_task_bg
                            )
                        }

                        2L -> { //已完成
                            stateTv.text = StringUtils.getString(R.string.task_completed)
                            stateTv.setTextColor(ColorUtils.getColor(com.polaris.live.R.color.color_000000_30))
                            state2Tv.setTextColor(ColorUtils.getColor(com.polaris.live.R.color.color_000000_30))
                            stateTv.setBackgroundResource(com.polaris.live.R.drawable.r20_shape_000000_30_stork)
                        }
                    }
                }

            }
            mBinding.rewardLl.setOnClickListener {
                item.rewards.getOrNull(0)?.let { da ->
                    clickBack?.invoke(da.rewardExplainType)
                }

            }
            mBinding.stateTv.setOnClickListener {
                goBack?.invoke(item)
            }
        }

        private var clickBack: ((explainType: Long) -> Unit)? = null
        private var goBack: ((reward: UserTask) -> Unit)? = null

        fun setItemListener(
            clickBack: ((explainType: Long) -> Unit)? = null,
            goBack: ((reward: UserTask) -> Unit)? = null,
        ) {
            this.clickBack = clickBack
            this.goBack = goBack
        }
    }

    class TaskCenterAnchorProgressViewHolder(
        binding: AdapterTaskAnchorProgressBinding,
    ) : BaseBindingViewHolder<AdapterTaskAnchorProgressBinding, UserTask>(binding) {
        override fun bind(item: UserTask) {
            mBinding.prView.setData(item)
            mBinding.prView.setItemListener({
                clickBack?.invoke(it)
            }, {
                goBack?.invoke(it)
            })
            if (absoluteAdapterPosition == 0) {
                mBinding.prView.setBackgroundResource(com.polaris.live.R.drawable.rb_12_fff)
            } else {
                mBinding.prView.setBackgroundResource(com.polaris.live.R.drawable.r12_back_ffffff)
            }

        }

        private var clickBack: ((explainType: Long) -> Unit)? = null
        private var goBack: ((reward: UserTask) -> Unit)? = null

        fun setItemListener(
            clickBack: ((explainType: Long) -> Unit)? = null,
            goBack: ((reward: UserTask) -> Unit)? = null,
        ) {
            this.clickBack = clickBack
            this.goBack = goBack
        }
    }

    private var clickBack: ((explainType: Long) -> Unit)? = null
    private var goBack: ((reward: UserTask) -> Unit)? = null

    fun setItemListener(
        clickBack: ((explainType: Long) -> Unit)? = null,
        goBack: ((reward: UserTask) -> Unit)? = null,
    ) {
        this.clickBack = clickBack
        this.goBack = goBack
    }

    companion object {
        const val TYPE_TASK_1 = 0               //默认
        const val TYPE_TASK_PROGRESS = 1               //进度类型

    }

}
